package rmiserver.service;

import java.rmi.RemoteException;
import java.rmi.server.RemoteServer;
import java.rmi.server.ServerNotActiveException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import java.util.List;

import rmiserver.ui.controller.DefaultController;
import rmiserver.ui.model.ConnectionBean;

public class RMIServiceImpl extends UnicastRemoteObject implements RMIService {

	private DefaultController controller;

	public RMIServiceImpl(DefaultController controller) throws RemoteException {
		super();
		this.controller = controller;
	}

	public String connect() throws RemoteException {
		try {
			this.controller.changeListOfConnection(RemoteServer.getClientHost());
		} catch (ServerNotActiveException e) {
			e.printStackTrace();
		}
		return "OK";
	}

	public boolean login(String username, String password) throws RemoteException {
		ConnectionBean bean;
		try {
			bean = new ConnectionBean(RemoteServer.getClientHost(), new Date().toString());
			bean.setUsername(username);
			this.controller.changeListOfLoggedUser(bean);
		} catch (ServerNotActiveException e) {
			e.printStackTrace();
		}
		return LoginService.login(username, password);
	}

	public List<String> getSharedFiles() throws RemoteException {
		DefaultFileSharing fileSharing = DefaultFileSharing.getInstance();
		return fileSharing.getSharedFiles();
	}

}
